package whisper.service.coder.qtfaststart;

import org.apache.commons.lang3.StringUtils;
import whisper.service.coder.Command;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by JT on 2018/5/7
 */
public class QtFaststartCommand implements Command {

    private static final String KEY_MEDIA_SRC = "MEDIA_SOURCE";
    private static final String KEY_MEDIA_TARGET = "KEY_MEDIA_TARGET";

    private Map<String, String> arguments = new HashMap<>();
    private StringBuilder argumentBuilder;

    public QtFaststartCommand(String utilPath, String src, String target) {
        this.argumentBuilder = new StringBuilder(utilPath);
        this.arguments.put(KEY_MEDIA_SRC, src);
        this.arguments.put(KEY_MEDIA_TARGET, target);
    }


    @Override
    public Command addArgument(String key, String value) {
        this.argumentBuilder.append(' ').append(key).append(' ').append(value).append(' ');
        return this;
    }

    @Override
    public Command addArgument(String value) {
        this.argumentBuilder.append(' ').append(value).append(' ');
        return this;
    }

    @Override
    public String builder() {

        /* 设置输入输出文件 */
        String srcPath = this.arguments.get(KEY_MEDIA_SRC);
        String targetPath = this.arguments.get(KEY_MEDIA_TARGET);
        if (StringUtils.isNotBlank(srcPath) && StringUtils.isNotBlank(targetPath)) {
            this.addArgument(srcPath).addArgument(targetPath);
        } else {
            throw new RuntimeException("QTFaststart: Media source path or target path is empty or null!");
        }

        return this.argumentBuilder.toString();
    }

}
